package com.kzd.btgame.main.home

import android.content.Context
import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import com.kzd.btgame.R
import com.kzd.btgame.custom.setOnClickAntiShakeListener
import com.kzd.btgame.databinding.ActivityBargainPaymentBinding
import com.kzd.btgame.entity.BargainDetails
import com.kzd.btgame.jetpack.vm.BargainViewModel
import com.kzd.btgame.main.KEY_BARGAIN_DETAILS_ENTITY
import com.kzd.btgame.main.popup.LoadingDialog
import com.kzd.btgame.main.toBargainDetailsActivity
import com.kzd.btgame.main.toMineRichTextActivityUrl
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.image.loadImage
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel
import java.lang.StringBuilder

class BargainPaymentActivity : OwnerViewBindingActivity<ActivityBargainPaymentBinding>() {

    private val mBargainDetails: BargainDetails?
        get() = intent?.extras?.getSerializable(KEY_BARGAIN_DETAILS_ENTITY) as? BargainDetails

    private var mPayType: Int = 1

    private val mViewModel:BargainViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityBargainPaymentBinding
        get() = ActivityBargainPaymentBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flBargainPaymentBar
                barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mBargainDetails?.let { fillData(it) }

        mViewBinding.tvBargainPaymentSubmit.setOnClickAntiShakeListener {
            val bargainDetails = mBargainDetails
                ?: return@setOnClickAntiShakeListener
            mViewModel.bargainPayment(mPayType, bargainDetails.id, bargainDetails.money)
        }

        mViewBinding.rbBargainPaymentPtb.isChecked = true

        mViewBinding.rbBargainPaymentPtb.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) mPayType = 1
        }

        mViewBinding.rbBargainPaymentWx.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) mPayType = 2
        }

        mViewBinding.rbBargainPaymentAli.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) mPayType = 3
        }
    }

    override fun initViewMode() {
        mViewModel.bargainPaymentLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.START) {
                LoadingDialog.show(this as Context, R.layout.dialog_load_style_1)
            } else if (response.status == Status.FAILURE) {
                LoadingDialog.dismiss()
                toast(response.error?.msg)
            } else {
                LoadingDialog.dismiss()
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val type = response.data?.type
                ?: return@observe
            if (type == 1) {
                toast(response.data?.message)
                toBargainDetailsActivity(
                    bargainId = response.data?.id ?: return@observe
                )
            } else {
                val data = response.data?.data
                    ?: return@observe
                toMineRichTextActivityUrl(
                    "支付", data.url
                )
            }
        })
    }

    private fun fillData(mBargainDetails: BargainDetails) {
        mViewBinding.ivBargainPayGameIcon.loadImage(mBargainDetails.icon)
        mViewBinding.tvBargainPayGameName.text = mBargainDetails.gname
        mViewBinding.tvBargainPayGameDesc.text = mBargainDetails.name
        mViewBinding.tvBargainPayGameArea.text = StringBuilder()
            .append("区服：").append(mBargainDetails.sname).append("\t实际充值：")
            .append(mBargainDetails.pay_money).append("元")
        mViewBinding.tvBargainPayGamePrice.text = StringBuilder().append("￥")
            .append(mBargainDetails.money)
    }
}